using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using WPFTemplateLib.WpfConverters.Core;

namespace WPFTemplateLib.WpfConverters
{
	/// <summary>
	/// [多值转换器] 判断列表并返回参数指定值（参数规则：【true返回值:false返回值:值类型(<see cref="ConvertResultType"/>的字符串形式)】）
	/// </summary>
	public class JudgeListReturnParaValueMultiConverter : MultiValueConverterBase
	{
		/// <summary>
		/// 判断列表的行为类型（默认值：AllTrue）
		/// </summary>
		public JudgeListActionType JudgeType { get; set; } = JudgeListActionType.AllTrue;

		/// <inheritdoc />
		public override object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
		{
			if(values == null || values.Length == 0)
				throw new ArgumentNullException(nameof(values), "values can not be null or empty");

			bool result = false;
			List<bool> boolList = ConverterHelper.ConvertValuesToBoolList(values);
			switch (JudgeType)
			{
				default:
				case JudgeListActionType.AllTrue:
					result = boolList.TrueForAll(b => b);
					break;
				case JudgeListActionType.AllFalse:
					result = boolList.All(b => !b);
					break;
				case JudgeListActionType.ContainTrue:
					result = boolList.Exists(b => b);
					break;
			}

			string[] paraArray = ConverterHelper.ConvertParaToTrueFalseValueAndType(parameter);
			string resultStr = result ? paraArray[0] : paraArray[1];
			return ConverterHelper.ConvertValue(resultStr, paraArray[2]);
		}
	}
}
